﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp2
{
    public partial class Form5 : Form
    {
        // 定义自定义坐标系的范围
        int CustomMinX = 70;
        int CustomMaxX = 360;
        int CustomMinY = 30;
        int CustomMaxY = 255;
        // 获取Panel的宽度和高度
        int rectWidth = 0;
        int rectHeight = 0;//相当于实际上就是panel1.Height-20个像素
        const int CustomInterval = 10; // X轴和Y轴等分为10份
        int X =0;
        int Y =0;
        /// <summary>
        /// 矩形顶端坐标
        /// </summary>
        Point Rectlacation = new Point(30, 30);

        public Form5()
        {
            InitializeComponent();
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

             Graphics g = e.Graphics;
             rectHeight = (panel1.Height-30)-50;
             rectWidth = (panel1.Width-30)-50;
            //// 在Panel控件中绘制一个矩形
            //矩形的左顶端坐标
          
            Rectangle rect = new Rectangle(Rectlacation.X, Rectlacation.Y, rectWidth,rectHeight);
            g.FillRectangle(Brushes.White, rect);
            //原点坐标
             X = rect.X;
             Y = rect.Y + rectHeight;

            // 绘制X轴
            g.DrawLine(Pens.Black,X,Y,X+rectWidth,Y);
            // 绘制Y轴
            g.DrawLine(Pens.Black, X, Y, rect.X, rect.Y);

            // 计算X轴刻度间隔
            float xInterval = rectWidth / (float)(CustomInterval - 1);



            // 绘制X轴刻度
            for (int i = 0; i < CustomInterval; i++)
            {
                float x = i * xInterval+X;
                g.DrawLine(Pens.Black, x, Y - 5, x, Y);
                double value = CustomMinX + i * ((CustomMaxX - CustomMinX) / (CustomInterval - 1));
                Console.WriteLine($"刻度值{i}个：{value}");
                g.DrawString((CustomMinX + i * (CustomMaxX - CustomMinX) / (CustomInterval - 1)).ToString(), DefaultFont, Brushes.Black, x, Y + 5);
            }

            float yInterval = rectHeight / (float)(CustomInterval - 1);

            // 绘制Y轴刻度
            for (int i = 0; i < CustomInterval; i++)
            {
                float y = Y - i * yInterval;
                g.DrawLine(Pens.Black, X, y, X + 5, y);
                g.DrawString((CustomMinY + i * (CustomMaxY - CustomMinY) / (CustomInterval - 1)).ToString(), DefaultFont, Brushes.Black,X-25, y);
            }
            DrawCustomLine();
        }

        private void DrawCustomLine()
        {
            using (Graphics g = panel1.CreateGraphics())
            {
                // 定义线段坐标
                Point[] customPoints = new Point[]
                {
                    new Point(70, 30),
                    //new Point(230, 78),
                    //new Point(280, 69),
                    //new Point(300, 70),
                    new Point(360,255)
                };

                // 转换自定义坐标为屏幕坐标
                Point[] screenPoints = new Point[customPoints.Length];
                for (int i = 0; i < customPoints.Length; i++)
                {
                    Console.WriteLine($"自定义坐标为{customPoints[i].X}，转化后坐标为{MapXToScreen(customPoints[i].X, panel1.Width)}");
                    screenPoints[i] = new Point(MapXToScreen(customPoints[i].X, rectWidth), MapYToScreen(customPoints[i].Y, rectHeight));
                }

                // 绘制线段
                g.DrawLines(Pens.Red, screenPoints);
            }
        }



        // 将自定义X坐标映射到屏幕坐标
        private int MapXToScreen(int customX, int rectWidth)
        {
            //return (int)((float)customX / (CustomMaxX - CustomMinX) * panelWidth);
            //float v = (float)customX / (CustomMaxX - CustomMinX)+X;

            return (int)((float)(customX- CustomMinX) / (CustomMaxX - CustomMinX) * rectWidth)+ Rectlacation.X;
        }

        // 将自定义Y坐标映射到屏幕坐标
        private int MapYToScreen(int customY, int rectHeight)
        {
            //return (int)((float)(CustomMaxY- (customY- CustomMinY)) / (CustomMaxY - CustomMinY) * rectHeight) + Rectlacation.Y;
            return (int)((float)((CustomMaxY - CustomMinY)- (customY - CustomMinY)) / (CustomMaxY - CustomMinY) * rectHeight) + Rectlacation.Y;
            
        }




        // 将屏幕X坐标映射到自定义坐标
        private int MapScreenToX(int screenX, int rectWidth)
        {
            return (int)((float)(screenX- Rectlacation.X) / rectWidth * (CustomMaxX - CustomMinX))+ CustomMinX;
        }

        // 将屏幕Y坐标映射到自定义坐标
        private int MapScreenToY(int screenY, int rectHeight)
        {
            return CustomMaxY - (int)((float)(screenY- Rectlacation.Y) / rectHeight * (CustomMaxY - CustomMinY));
        }

        private void panel1_MouseClick(object sender, MouseEventArgs e)
        {
            // 将屏幕坐标映射到自定义坐标系中
            int customX = MapScreenToX(e.X, rectWidth);
            int customY = MapScreenToY(e.Y, rectHeight);

            MessageBox.Show($"点击坐标：X={e.X}, Y={e.Y}\n对应自定义坐标：X={customX}, Y={customY}");



        }
    }
}
